import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:series_play/comm/base/base_state.dart';
import 'package:series_play/widget/app_color.dart';
import 'package:series_play/widget/app_image.dart';
import 'package:series_play/widget/app_refresh_view.dart';
import 'package:series_play/widget/app_text.dart';

import 'controller/home_for_you_controller.dart';

/// 首页推荐列表
class HomeForYouPage extends StatefulWidget {
  const HomeForYouPage({super.key});

  @override
  State<HomeForYouPage> createState() => _HomeForYouPageState();
}

class _HomeForYouPageState
    extends BaseControllerState<HomeForYouPage, HomeForYouController> {
  @override
  void initState() {
    super.initState();
    mController.onRefresh();
  }

  @override
  Widget buildPage(BuildContext context) {
    final itemWidth = (MediaQuery.of(context).size.width - 18.w * 2 - 20.w) / 3;
    final imgHeight = itemWidth * 5 / 4;
    return AppRefreshGridView(
      padding: const EdgeInsets.symmetric(horizontal: 18).w,
      listController: mController,
      crossAxisCount: 3,
      crossAxisSpacing: 10.w,
      mainAxisSpacing: 10.w,
      itemBuilder: (context, index) {
        final item = mController.dataList[index];
        return GestureDetector(
          onTap: () {
            mController.onDramaInfo(item.diId ?? -1);
          },
          behavior: HitTestBehavior.translucent,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ClipRRect(
                borderRadius: BorderRadius.circular(8).r,
                child: AppImage(
                  item.cover ?? '',
                  width: itemWidth,
                  height: imgHeight,
                  boxFit: BoxFit.cover,
                ),
              ),
              8.verticalSpace,
              AppText(
                item.name ?? '',
                color: AppColor.white,
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
            ],
          ),
        );
      },
      onRefresh: mController.onRefreshing,
      onLoad: mController.onLoadingMore,
    );
  }

  @override
  HomeForYouController createController() {
    return HomeForYouController();
  }

  @override
  bool hideAppbar() {
    return true;
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Color getBackgroundColor() {
    return Colors.transparent;
  }
}
